package com.bwie.service.impl;

import cn.hutool.core.util.RandomUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bwie.mapper.TbCommodityMapper;
import com.bwie.mapper.TbOrderMapper;
import com.bwie.pojo.TbCommodity;
import com.bwie.pojo.TbOrder;
import com.bwie.pojo.TbUser;
import com.bwie.service.TbUserService;
import com.bwie.mapper.TbUserMapper;
import com.bwie.utils.R;
import com.bwie.vo.LoginInfoVo;
import com.bwie.vo.PageInfoVo;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.concurrent.TimeUnit;

/**
* @author Yoo19
* @description 针对表【tb_user】的数据库操作Service实现
* @createDate 2025-06-26 09:17:43
*/
@Service
public class TbUserServiceImpl extends ServiceImpl<TbUserMapper, TbUser>
    implements TbUserService{
    @Autowired
    TbUserMapper tbUserMapper;
    @Autowired
    StringRedisTemplate redisTemplate;
    @Autowired
    TbCommodityMapper tbCommodityMapper;
    @Autowired
    TbOrderMapper tbOrderMapper;

    @Override
    public R login(LoginInfoVo loginInfoVo) {
        //通过手机号判断用户是否存在
        TbUser user = tbUserMapper.selectOne(new LambdaQueryWrapper<TbUser>().eq(TbUser::getUserPhone, loginInfoVo.getUserPhone()));
        if(user == null){
            return R.ERROR(500,"该用户不存在");
        }

        if(!user.getUserPass().equals(loginInfoVo.getUserPass())){
            return R.ERROR(500,"密码错误");
        }

        //生成token
        String token = Jwts.builder().signWith(SignatureAlgorithm.HS256, "123456")
                .claim("userId", user.getUserId())
                .compact();
        String redisKey = "token-" + user.getUserId();
        redisTemplate.opsForValue().set(redisKey,token,60, TimeUnit.MINUTES);


        user.setUserPass(null);
        user.setToken(token);


        return R.OK(user);
    }

    @Override
    public R getCommodityList(PageInfoVo pageInfoVo) {
        Page<TbCommodity> page = new Page<>(pageInfoVo.getPageNum(), pageInfoVo.getPageSize());
        Page<TbCommodity> tbCommodityPage = tbCommodityMapper.getCommodityList(page,pageInfoVo);

        return R.OK(tbCommodityPage);
    }

    @Override
    public R getOrderList() {
      List<TbOrder> orderList = tbOrderMapper.getOrderList();
        return R.OK(orderList);
    }

    @Override
    public R addCommodity(TbCommodity tbCommodity) {
        //判断名称是否存在
        TbCommodity tbCommodity1 = tbCommodityMapper.selectOne(new LambdaQueryWrapper<TbCommodity>().eq(TbCommodity::getCommodityName, tbCommodity.getCommodityName()));
        if(tbCommodity1 != null){
            return R.ERROR(500,"该商品已存在,请勿重复添加");
        }

        if(tbCommodity.getDjcd() == 1 && tbCommodity.getYfx() == 1){
            tbCommodity.setTag("放心购");
        }else {
            tbCommodity.setTag("无");
        }

        String s = RandomUtil.randomNumbers(4);
        tbCommodity.setCommodityCode(s);
        tbCommodityMapper.insert(tbCommodity);
        return R.OK();
    }
}




